package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/* 业务层实现类需要添加@Service注解,表示当前类为业务层实现类*/
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("UserService:用户登录业务:userLoginParam={}", userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if (userVO == null) {
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        log.debug("UserService:保存用户业务:userSaveParam={}", userSaveParam);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam, user);
        // 根据ID是否为null,判断是添加还是修改,如果是null,则添加,否则修改
        if (user.getId() == null) {
            user.setPassword("123456");
            user.setCreateTime(new Date());
            userMapper.insertaddUser(user);
        } else {
            //TODO:修改
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public List<UserVO> selectAll(UserQuery userQuery) {
        log.debug("UserService:根据用户名查询用户业务:userQuery={}", userQuery);
        List<UserVO> UserVOlist = userMapper.selectAll(userQuery);
        return UserVOlist;
    }

    @Override
    public void update(Long userId) {
        log.debug("UserService:修改密码业务:userId={}", userId);
        User user = new User();
        user.setId(userId);
        user.setPassword("root");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long userId, String status) {
        log.debug("UserService:修改状态业务:userId={},status={}", userId, status);
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Long userId) {
        log.debug("UserService:删除用户业务:userId={}", userId);
        userMapper.deleteUser(userId);
    }

    @Override
    public List<UserVO> selectAudit(Long parentId) {
        log.debug("UserService:根据上级id查询用户业务:parentId={}", parentId);
        //1.准备一个空集合,用来保存查出来的多个审批人
        ArrayList<UserVO> list = new ArrayList<>();
        //2.根据传过来的上级ID,先把直属领导查出来
        UserVO userid1 = userMapper.selectByParentId(parentId);
        list.add(userid1);
        if(userid1 !=null && userid1.getParentId() !=null){
            UserVO userid2 = userMapper.selectByParentId(userid1.getParentId());
            list.add(userid2);
        }
        return list;
    }
}
